﻿using System.Data;
using System;
using System.Web.UI.WebControls;

namespace BrsdServer
{
    public partial class userList : System.Web.UI.Page
    {
        userInfoBLL user = new userInfoBLL();
        DataTable userDT = new DataTable();   //读取数据库中的数据到DataTable结构中。
        int rowNum = 0;   //记录有多少条记录
        private static string userName = "";
        private static string userPwd = "";
        private void GVDataBind()
        {
            if (user.GetUserKind(userName,userPwd)==10)
            {
                userDT = user.LoadAllDataTable();
            }
            else
            {
                userDT = user.LoadAllDataTable(userName);
            }
            
            gvUsrInfo.DataSource = userDT;
            gvUsrInfo.DataBind();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if ((String)Session["nameSession"] == "" || (String)Session["pwdSession"] == ""
                    || Session["nameSession"] == null || Session["pwdSession"] == null)
                {
                    Response.Redirect("index.aspx");
                    return;
                }
                userName = (String)Session["nameSession"];
                userPwd = (String)Session["pwdSession"];
                GVDataBind();
            }
        }
        protected void gvUsrInfo_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
        {
            int i;
            //执行循环，保证每条数据都可以更新
            for (i = -1; i < gvUsrInfo.Rows.Count; i++)
            {
                //首先判断是否是数据行
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    //当鼠标停留时更改背景色
                    e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");
                    //当鼠标移开时还原背景色
                    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                }
                //根据表中的权限数据据来填充DataGrid 10：管理员 0：普通用户 5:代理商
            }
            if (e.Row.RowIndex != -1)
            {
                rowNum = e.Row.RowIndex + 1;
                e.Row.Cells[0].Text = rowNum.ToString();
                if (userDT.Rows[i][3].ToString() == "10")
                {
                    e.Row.Cells[2].Text = "管理员";
                }
                else if(userDT.Rows[i][3].ToString()=="0")
                {
                    e.Row.Cells[2].Text = "普通用户";
                }
                else
                {
                    e.Row.Cells[2].Text = "代理商";
                }
            }
        }

        protected void gvUsrInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            // 得到该控件
            GridView theGrid = sender as GridView;
            int newPageIndex = 0;
            if (e.NewPageIndex == -3)
            {
                //点击了Go按钮
                TextBox txtNewPageIndex = null;
                GridViewRow pagerRow = theGrid.BottomPagerRow;
                if (pagerRow != null)
                {
                    txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;
                }
                if (txtNewPageIndex != null)
                {
                    //得到索引
                    newPageIndex = int.Parse(txtNewPageIndex.Text) - 1;
                }
            }
            else
            {
                //点击了其他的按钮
                newPageIndex = e.NewPageIndex;
            }
            //防止新索引溢出
            newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
            newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;
            //得到新的值
            theGrid.PageIndex = newPageIndex;
            //重新绑定
            GVDataBind();
        }

        //protected void gvUsrInfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
        //{
        //    string userName = gvUsrInfo.Rows[e.RowIndex].Cells[1].Text;
        //    if (userName == "Admin")
        //    {
        //        string strScript = "<script>alert('管理员用户不能删除!')</script>";
        //        Page.ClientScript.RegisterStartupScript(this.GetType(), "", strScript);
        //        return;
        //    } 
        //    else
        //    {
        //        user.DeleteUser(userName);
        //        GVDataBind();
        //    }
        //}
    }
}
